const compose = require('koa-compose');
const glob = require('glob')
const { resolve } = require('path')

/**
 * 此文件不要动
 * 所有的router文件需要以Router结尾，并且需要存放在routers文件夹下，
 * 可以是每个摸块下的routers文件夹或其子文件夹中
 * 例如
 *      module/demo/routers/aaRouter.js
 *      module/demo/routers/user/aaRouter.js
 **/
registerRouter = () => {
    let routers = [];
    glob.sync(resolve(__dirname, './', '**/routers/**/**Router.js'))
        .filter(value => (value.indexOf('routers.js') === -1))
        .map(router => {
            routers.push(require(router).routes())
            routers.push(require(router).allowedMethods())
        })
    return compose(routers)
}

module.exports = registerRouter
